package com.neusoft.utils.jsp;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.PageContext;

import jodd.util.ReflectUtil;

import com.neusoft.utils.springmvc.SpringContextUtils;

public class BeanTag extends ValueTag {


    
	@Override
	public void work(PageContext pageContext) { 
        String[] works=work.split("\\.");
		Object result = null;
		Object[] param=getParam();
		try {
			//ReflectUtil.invoke(obj, method, paramClasses, params);
			result = ReflectUtil.invoke(SpringContextUtils.getBean(works[0]), works[1], param);
		} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		pageContext.setAttribute(alias, result);
	}
   private Object[] getParam(){
	   List<Object> list= new ArrayList<Object>();
	   if(null!=p1){
		   list.add(p1);
	   }
	   if(null!=p2){
		   list.add(p2);
	   }
	   if(null!=p3){
		   list.add(p3);
	   }
	   if(null!=p4){
		   list.add(p4);
	   }
	   if(null!=p5){
		   list.add(p5);
	   }
	   if(null!=p6){
		   list.add(p6);
	   }
	   if(null!=p7){
		   list.add(p7);
	   }
	   if(null!=p8){
		   list.add(p8);
	   }
	   if(null!=p9){
		   list.add(p9);
	   }
	  if(list.isEmpty()){
		  return null;
	  }
	  return list.toArray(new Object[list.size()]);
   }
	/**
	 *  
	 */
	private static final long serialVersionUID = -7212705156682934668L;
	private String work;
    private String alias;
	private Object p1;
	private Object p2;
	private Object p3;
	private Object p4;
	private Object p5;
	private Object p6;
	private Object p7;
	private Object p8;
	private Object p9;
	public String getWork() {
		return work;
	}
	public void setWork(String work) {
		this.work = work;
	}
	public String getAlias() {
		return alias;
	}
	public void setAlias(String alias) {
		this.alias = alias;
	}
	public Object getP1() {
		return p1;
	}
	public void setP1(Object p1) {
		this.p1 = p1;
	}
	public Object getP2() {
		return p2;
	}
	public void setP2(Object p2) {
		this.p2 = p2;
	}
	public Object getP3() {
		return p3;
	}
	public void setP3(Object p3) {
		this.p3 = p3;
	}
	public Object getP4() {
		return p4;
	}
	public void setP4(Object p4) {
		this.p4 = p4;
	}
	public Object getP5() {
		return p5;
	}
	public void setP5(Object p5) {
		this.p5 = p5;
	}
	public Object getP6() {
		return p6;
	}
	public void setP6(Object p6) {
		this.p6 = p6;
	}
	public Object getP7() {
		return p7;
	}
	public void setP7(Object p7) {
		this.p7 = p7;
	}
	public Object getP8() {
		return p8;
	}
	public void setP8(Object p8) {
		this.p8 = p8;
	}
	public Object getP9() {
		return p9;
	}
	public void setP9(Object p9) {
		this.p9 = p9;
	}
   
}
